/**
 * Created by jiali on 2017/2/24.
 */

var retUrl;

$(function () {

    if($(".layui-layer-content").length>0) {
        location.href = '../Login/Index';
    }
    retUrl = frxs.getUrlParam("returl");

    init();
    judgeIE();
    clearCookie();
    $("#UserAccount").val($.cookie("UserAccount"));
    $("#Password").val("");
});


//初始化
function init() {
    $("#btnLogin").click(function () {
        login();
    });

    //回车登录
    document.onkeydown = function(e) {
        var ev = document.all ? window.event : e;
        if (ev.keyCode == 13) {
            login();
        }
    };
}


//清空购物车Cookie
function clearCookie() {
    //过期时间 -30分钟
    var date = new Date();
    date.setTime(date.getTime() - (30 * 60 * 1000));

    //清空
    $.cookie("cartCount", "", { path: "/", expires: date });
    //清空
    $.cookie("isNewMessage", "", { path: "/", expires: date });
}

//登录
function login() {


    //验证
    $("#msg").html("&nbsp;");
    var validate = frxs.validateForm();
    var data = $("#myForm").serializeArray();
    if (validate) {
        $("#btnLogin").val("登录中..");
        $("#btnLogin").attr("disabled", "disabled");
        $.ajax({
            url: "http://127.0.0.1/zx7c/pcorder/passport/login",
            type: "post",
            data: data,
            dataType: "text",
            success: function (obj) {
                //是否勾选
                if ($("#check:checked").length > 0) {
                    $.cookie("UserAccount", $("#UserAccount").val(), { path: '/', expires:360});
                } else {
                    $.cookie("UserAccount", "", { path: "/", expires: 0 });
                };
                $("#btnLogin").removeAttr("disabled");
                $("#btnLogin").val("登录");
                if (obj.Flag != "0") {
                    $("#msg").text(obj.Info);
                    refreshCode();
                } else {
                    $("#btnLogin").val("登录成功");
                    //如果就一条门店数据后台自动判断并已经登录
                    if (obj.code == "200") {
                        //加载层
                        layer.load(0, { shade: [0.3, '#000'] });
                        //if (retUrl) {
                        //因为需要首页验证结算单这里修改成不跳到retUrl页面了
                        //location.href = decodeURIComponent(retUrl);
                        //} else {
                        location.href = "http://127.0.0.1/zx7c/pcorder/index/index";
                        //}
                    } else {
                        if (obj.ShopList.length > 1 || (obj.ShopList.length==1 && obj.ShopList[0].Status != 1)) {
                            $(obj.ShopList).each(function () {
                                //冻结
                                if (this.Status == "0") {
                                    $("#shopList").append("<li id=\"" + this.ShopID + "\" onclick='selShop(\"" + obj.UserId + "\",\"" + obj.UserName + "\",\"" + this.ShopID + "\",\"" + this.ShopName + "\",\"" + this.WID + "\",\"" + this.MininumAmt + "\")'>" + this.ShopName + "&nbsp;&nbsp;（冻结）</li>");
                                }
                                //淘汰
                                else if (this.Status == "2") {
                                    $("#shopList").append("<li class='traw' style='cursor: default'>" + this.ShopName + "&nbsp;&nbsp;（淘汰）</li>");
                                } else{
                                    $("#shopList").append("<li id=\"" + this.ShopID + "\" onclick='selShop(\"" + obj.UserId + "\",\"" + obj.UserName + "\",\"" + this.ShopID + "\",\"" + this.ShopName + "\",\"" + this.WID + "\",\"" + this.MininumAmt + "\")'>" + this.ShopName + "</li>");
                                }

                            });
                            $(".dialog").show();
                        } else if (obj.ShopList.length == 1) {
                            selShop("\"" + obj.UserId + "\",\"" + obj.UserName + "\",\"" + obj.ShopList[0].ShopID + "\",\"" + obj.ShopList[0].ShopName + "\",\"" + obj.ShopList[0].WID + "\",\"" + obj.ShopList[0].MininumAmt + "\"");
                        } else {
                            $("#msg").text("没有可用的门店");
                        }
                    }
                }
            }
        });
    }
}

//选择
function selShop(userId, userName, shopId, shopName, wid, mininumAmt) {
    $("#shopList li").removeAttr("style");
    $("#" + shopId).attr("style", "background:#E50014;color:#fff");

    var data = {
        userId: userId,
        userName: userName,
        shopId: shopId,
        shopName: shopName,
        wid: wid,
        mininumAmt: mininumAmt
    };
    //加载层
    layer.load(0, { shade: [0.3, '#000'] });
    $.ajax({
        url: "../Login/SelectShopHandle",
        type: "post",
        data: data,
        dataType: "json",
        success: function (obj) {
            if (obj.Flag == "0") {
                //if (retUrl) {
                //因为需要首页验证结算单这里修改成不跳到retUrl页面了
                //location.href = decodeURIComponent(retUrl);
                //} else {
                //location.href = "../Home/Index?first=true";
                //}
                location.href = "../Home/Index?first=true";
            } else {
                layer.closeAll();
                $("#msg").text("登录异常");
            }
        }
    });
}


//刷新页面
function refreshCode() {
    var img = document.getElementById("imgVerifyCode");
    if (img != null) {
        var currentDate = new Date();
        img.src = '../Login/VerifyCode' + '?t=' + currentDate.getTime();
    }
}


/*当浏览器为IE6、7时，弹出升级浏览器提示框*/
function judgeIE() {
    if (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.split(";")[1].replace(/[ ]/g, "") == "MSIE6.0") {
        $(window).load(function () {
            $(".pay_bg").show();
            $(".IEpop").show();
        })
    }
    else if (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.split(";")[1].replace(/[ ]/g, "") == "MSIE7.0") {
        $(window).load(function () {
            $(".pay_bg").show();
            $(".IEpop").show();
        })
    }

}
/*关闭弹窗*/
function closeIEpop() {
    $(".pay_bg").hide();
    $(".IEpop").hide();
}